module.exports = function(grunt) {

  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-jasmine');
  grunt.loadNpmTasks('grunt-contrib-watch');

  // Project configuration.
  grunt.initConfig({
    modules: [],//to be filled in by build task
    pkg: grunt.file.readJSON('package.json'),
    dist: 'dist',
    filename: 'jsturbo',
    meta: {
      banner: ['/*',
               ' * <%= pkg.name %>',
               ' * <%= pkg.homepage %>',
               ' * Version: <%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>',
               ' */\n'].join('\n'),
      srcFiles: [ //ordem importa por causa das dependências!
          'src/jsturbo.string.js',
          'src/jsturbo.number.js',
          'src/jsturbo.array.js',
          'src/jsturbo.date.js',
          'src/jsturbo.time.js',
          'src/jsturbo.input.util.js',
          'src/jsturbo.jquery.js',
          'src/jsturbo.object.js'
        ],
      specFiles: [
          'spec/jsturbo.string-spec.js',
          'spec/jsturbo.number-spec.js',
          'spec/jsturbo.array-spec.js',
          'spec/jsturbo.date-spec.js',
          'spec/jsturbo.time-spec.js',
          'spec/jsturbo.input.util-spec.js',
          'spec/jsturbo.object-spec.js'
        ]
    },

    jasmine: {
      pivotal: {
        src: '<%= meta.srcFiles %>',
        options: {
          specs: '<%= meta.specFiles %>',
          vendor: [ 
            "external/jquery-1.11.1.min.js"
          ],
          keepRunner: true
        }
      }
    },
    
    concat: {
      latest: {
        options: {
          banner: '<%= meta.banner %>'
        },
        src: '<%= meta.srcFiles %>',
        dest: '<%= dist %>/<%= filename %>-latest.js'
      },
      dist: {
        options: {
          banner: '<%= meta.banner %>'
        },
        src: '<%= meta.srcFiles %>',
        dest: '<%= dist %>/<%= filename %>-<%= pkg.version %>.js'
      }
    },

    uglify: {
      options: {
        banner: '<%= meta.banner %>'
      },
      latest:{
        src:['<%= concat.latest.dest %>'],
        dest:'<%= dist %>/<%= filename %>-latest.min.js'
      },
      dist:{
        src:['<%= concat.dist.dest %>'],
        dest:'<%= dist %>/<%= filename %>-<%= pkg.version %>.min.js'
      }
    },

    watch: {
      scripts: {
        files: ['Gruntfile.js', 'src/*.js', 'spec/*.js'],
        tasks: ['default'],
        options: {
          spawn: false,
        },
      },
    },

  });

  // Default task(s).
  grunt.registerTask('default', ['jasmine', 'concat:latest', 'uglify:latest']);
  
  grunt.registerTask('close', ['jasmine', 'concat:dist', 'uglify:dist', 'bump:patch']);

  grunt.registerTask('bump', 'Bumps the version', function(bumpType) {
    var VERSION_REGEXP = /(\d+)\.(\d+)\.(\d+)/i;
    var file = 'package.json';
    var content = grunt.file.read(file).replace(VERSION_REGEXP, function(match, major, minor, patch) {
      if(bumpType === 'major') {
        return (parseInt(major)+1) +'.0.0';
      } if(bumpType === 'minor') {
        return major + '.' + (parseInt(minor)+1) +'.0';
      }
      return major +'.'+ minor +'.'+ (parseInt(patch)+1);
    });
    grunt.file.write(file, content);
  });
};


